<?php
/**
 * User: 五哥派
 * Date: 2024/7/1
 * Tel(同WX): 17335592719
 */
namespace app\api\model\store;

use app\api\model\Base;

class Order extends Base{

    protected $name = 'store_order';

    protected $autoWriteTimestamp = true;

    // order_status 订单状态 0未付款 1已付款/待发货 2已发货/待收货 3已收货/待评价、已评价/已完成   -1退款/退款退货申请   -2已退款/已退款退货 -3拒绝 -4取消交易 -5关闭交易
    // pay_status 支付状态：0未支付 1已支付
    // delivery_status 发货状态(0未发货 1已发货 2部分发货)
    // receipt_status 收货状态：0未收货 1已收货
    public function getOrderStatusTxtAttr($value,$data){
        $order_status = [0=>'待付款',1=>'待发货', 2=>'待收货', 3=>'待评价', 4=>'已完成', -1=>'退款申请',-2=>'已退货',-3=>'拒绝', -4=>'取消交易', -5=>'关闭交易'];
        /*if(($data['order_status'] == 0) && ($data['pay_status'] == 0)){
            return '待付款';
        }elseif(($data['order_status'] == 1) && ($data['delivery_status'] == 0)){
            return '待发货';
        }elseif(($data['order_status'] == 2) && ($data['receipt_status'] == 0)){
            return '待收货';
        }elseif(($data['order_status'] == 3) && ($data['is_comment'] == 0)){
            return '待评价';
        }elseif(($data['order_status'] == 3) && ($data['is_comment'] == 1)){
            return '已完成';
        }elseif($data['order_status'] < 0){
            return $order_status[$data['order_status']];
        }*/

        return $order_status[$data['order_status']];
    }

    public function getPayStatusTxtAttr($value,$data){
        $pay_status = [0=>'未支付',1=>'已支付'];
        return $pay_status[$data['pay_status']];
    }

    public function orderitem(){
        return $this->hasMany('OrderItem', 'order_id', 'id');
    }

    public function orderaddress(){
        return $this->hasOne('OrderAddress', 'order_id', 'id');
    }

    //根据更新时间排序
    public function getList($where = array(), $page = 1, $size = 15){
        $list = self::with([
            'orderitem' => function($query){
                $query->field('id, order_id, sku_id, goods_id, num, price, name, image, props_text');
            }])
            ->where($where)
            ->field('id,order_no,user_id,total_price,user_note,admin_note,is_comment,order_status,delivery_status,total_num,create_time')
            ->order(["update_time" => "desc"])
            ->paginate($size, true, ['page' => $page]);
        return $list;
    }

    public function getDetail($where = array()){

        $field = 'id,order_no,user_id,total_price,pay_price,user_note,admin_note,is_comment,total_num,pay_type,trade_id,
        order_status,pay_status,delivery_status,receipt_status,is_comment,
        FROM_UNIXTIME(pay_time,"%Y-%m-%d %H:%i:%s") as pay_time,
        create_time';

        return self::with([
            'orderaddress',
            'orderitem' => function($query){
                $query->field('id,order_id,sku_id,goods_id,num,price,name, image, props_text,refund_type');
            }])
            ->where($where)
            ->field($field)
            ->find();
        /*return self::with(['shop','orderaddress',
            'ordergoods'=>function($query){$query->field('id,order_id,sku_id,goods_id,num,price,name, image,props_text');}])
            ->where($where)
            ->field('id,order_no,user_id,total_price,pay_price,user_note,admin_note,is_comment,total_num,
        order_status,order_status as order_status_txt,pay_type as pay_type_txt,send_time,confirm_time,pay_type,
        FROM_UNIXTIME(pay_time,"%Y-%m-%d %H:%i:%s") as pay_time,
        FROM_UNIXTIME(close_time,"%Y-%m-%d %H:%i:%s") as close_time,
        FROM_UNIXTIME(send_time,"%Y-%m-%d %H:%i:%s") as send_time,
           FROM_UNIXTIME(diniout_time,"%Y-%m-%d %H:%i:%s") as diniout_time,
         FROM_UNIXTIME(confirm_time,"%Y-%m-%d %H:%i:%s") as confirm_time,
         FROM_UNIXTIME(shipping_end_time,"%Y-%m-%d %H:%i:%s") as shipping_end_time,
         FROM_UNIXTIME(shipping_time,"%Y-%m-%d %H:%i:%s") as shipping_time,
        prepay_id,create_time')
            ->find();*/

    }




    /**
     * 获取当前用户订单数量
     * @param string $dataType 订单类型 (all全部 payment待付款 deliver待发货 received待收货 comment待评价)
     * @return int
     */
    public function getCount($userId, string $dataType = 'all')
    {
        //order_status订单状态 0未付款 1已付款/待发货 2已发货/待收货 3已收货/待评价/已完成      -1退款/退款退货申请         -2已退款/已退款退货 -3拒绝 -4取消交易 -5关闭交易

        // 设置订单类型条件
        $dataTypeFilter = $this->getFilterDataType($dataType);
        return $this->where('user_id', '=', $userId)
            ->where($dataTypeFilter)
            ->count();
    }

    /**
     * 设置订单类型条件
     * @param string $dataType
     */
    private function getFilterDataType(string $dataType)
    {
        //order_status订单状态 0未付款 1已付款/待发货 2已发货/待收货 3已收货/待评价 4已评价/已完成      -1退款/退款退货申请         -2已退款/已退款退货 -3拒绝 -4取消交易 -5关闭交易
        // 筛选条件
        $filter = [];
        // 订单数据类型
        switch ($dataType) {
            case 'all':
                break;
            case 'payment':
                $filter['pay_status'] = 0;
                $filter['order_status'] = 0;
                //$filter[] = ['pay_status', '=', 0];//待支付
                //$filter[] = ['order_status', '=', 0];//待支付
                break;
            case 'delivery':
                $filter['pay_status'] = 1;
                $filter['delivery_status'] = ['in', '0,2'];//发货状态(0未发货 1已发货 2部分发货)
                $filter['order_status'] = 1;
                /*$filter = [
                    ['pay_status', '=', 1],//支付成功
                    ['delivery_status', '<>', 10],//10未发货
                    ['order_status', '=', 1]
                ];*/
                break;
            case 'received':
                /*$filter = [
                    ['pay_status', '=', 1],
                    ['delivery_status', '=', 1],
                    ['receipt_status', '=', 0],
                    ['order_status', '=', 2]
                ];*/
                $filter['pay_status'] = 1;
                $filter['delivery_status'] = 1;
                $filter['receipt_status'] = 0;
                $filter['order_status'] = 2;
                break;
            case 'comment':
                /*$filter = [
                    ['is_comment', '=', 0],
                    ['order_status', '=', 3]
                ];*/
                $filter['is_comment'] = 0;
                $filter['order_status'] = 3;
                break;
        }
        return $filter;
    }

}